package com.elite.groovy.callback


import com.elite.groovy.util.EliteBean
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
/**
 * 在线监控页面定时任务
 * 2021/8/5
 */

public class apiCallMonitor implements ServiceBackInvoke {
    private static final Logger log = LoggerFactory.getLogger(apiCallMonitor.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        long startTime = System.currentTimeMillis();
        long endTime = System.currentTimeMillis();

        log.debug("3Vjia电话监控页面统计定时任务启动任务content...");
        try {
            // 当前-接通量/未接通量/平均排队时长/最长排队时间/呼入总量/接通率
            String selectSql = '''
                                Select Date_Format(R1.Callstarttime, '%Y-%m-%d') As monitor_date,
                                       Date_Format(R1.Callstarttime, '%H') As monitor_date_hour,
                                       Count(*) As total_call_num,
                                       Sum(Case
                                             When R1.Callestablished = 1 Then
                                              1
                                             Else
                                              0
                                           End) As completed_num,
                                       Sum(Case
                                             When R1.Callestablished = 0 Then
                                              1
                                             Else
                                              0
                                           End) As abandoned_num,
                                       Sum(Case
                                             When R2.His_No Is Not Null Then
                                              1
                                             Else
                                              0
                                           End) As wait_num,
                                       Sum(Case
                                             When R2.His_No Is Not Null Then
                                              R2.Waittime
                                             Else
                                              0
                                           End) As total_wait_time,
                                       Max(Ifnull(R2.Waittime, 0)) As max_wait_time
                                  From Event R1
                                  Left Join Trc2_Queue_Wait_His R2
                                    On R1.Attacheddata = R2.Uniqueid
                                   And R2.His_Time >= Date_Format(Now(), '%Y-%m-%d')
                                 Where R1.Calltype = 1
                                   And R1.Callstarttime >= Date_Format(Now(), '%Y-%m-%d')
                                 Group By Date_Format(R1.Callstarttime, '%Y-%m-%d'), Date_Format(R1.Callstarttime, '%H')
                                ''';

            List selectList = eliteBean.chaxun_sql(selectSql, "dataSource");
            endTime = System.currentTimeMillis();
            log.debug("3Vjia电话监控页面统计定时任务查询返回-耗时：" + (System.currentTimeMillis() - startTime) + "【长度】:" + selectList.size() + "【结果List】：" + selectList.toString());

            String[] doSaveResultSQL = new String[selectList.size()];

            int k = 0;
            for (int i = 0; i < selectList.size(); i++) {

                Map selectMap = selectList.get(i);

                String 	Data_Fd	 = selectMap.get("monitor_date").toString();
                String 	Hour_Fd	 = selectMap.get("monitor_date_hour").toString();
                String 	total_call_num	 = selectMap.get("total_call_num").toString();
                String 	completed_num	 = selectMap.get("completed_num").toString();
                String 	abandoned_num	 = selectMap.get("abandoned_num").toString();
                String 	wait_num	 = selectMap.get("wait_num").toString();
                String 	total_wait_time	 = selectMap.get("total_wait_time").toString();
                String 	max_wait_time	 = selectMap.get("max_wait_time").toString();

                log.debug("3Vjia电话监控页面统计定时任务当前处理Data_Fd：" + Data_Fd + " Hour_Fd:" + Hour_Fd);

                String selectCheck = "select id from 3vj_Call_Online_Monitor where monitor_date = '" + Data_Fd + "' and monitor_date_hour = '" + Hour_Fd + "'";

                List selectCheckList = eliteBean.chaxun_sql(selectCheck, "dataSource");
                if (selectCheckList.size() > 0) {
                    Map selectCheckMap = selectCheckList.get(0);
                    String m_ID = selectCheckMap.get("id");

                    // 更新当前时间 记录
                    String upSql = "Update 3vj_Call_Online_Monitor" +
                            "   Set total_call_num     = '" + total_call_num + "'," +
                            "       completed_num     = '" + completed_num + "'," +
                            "       abandoned_num     = '" + abandoned_num + "'," +
                            "       wait_num     = '" + wait_num + "'," +
                            "       total_wait_time     = '" + total_wait_time + "'," +
                            "       max_wait_time         = '" + max_wait_time + "'," +
                            "       Monitor_Time        = Now()" +
                            " Where Id = '" + m_ID + "'";
                    log.debug('更新当前时间的记录SQL' + upSql);
                    doSaveResultSQL[i] = upSql;
                } else {

                    // 插入该条记录
                    String insertSql = "Insert Into 3vj_Call_Online_Monitor" +
                            "  (Id," +
                            "   total_call_num," +
                            "   completed_num," +
                            "   abandoned_num," +
                            "   wait_num," +
                            "   total_wait_time," +
                            "   max_wait_time," +
                            "   Monitor_Date," +
                            "   Monitor_Date_Hour," +
                            "   Monitor_Time)" +
                            " Values" +
                            "  (Uuid()," +
                            "   '" + total_call_num + "'," +
                            "   '" + completed_num + "'," +
                            "   '" + abandoned_num + "'," +
                            "   '" + wait_num + "'," +
                            "   '" + total_wait_time + "'," +
                            "   '" + max_wait_time + "'," +
                            "   '" + Data_Fd + "'," +
                            "   '" + Hour_Fd + "'," +
                            "   Now())";
                    log.debug('插入当前时间的记录SQL' + insertSql);
                    doSaveResultSQL[i] = insertSql;

                }

                k = k + 1;
            }

            if (k > 0) {
                List doSaveResultSQLList = eliteBean.updateBatch(doSaveResultSQL, "dataSource");
                log.debug("3Vjia电话监控页面统计定时任务处理End-耗时:" + (System.currentTimeMillis() - endTime) + " 结果:" + doSaveResultSQLList.toString());
            } else {

                log.warn("3Vjia电话监控页面统计定时任务处理End...SQL空");
            }

            log.debug("3Vjia电话监控页面统计定时任务处理结束-总耗时:" + (System.currentTimeMillis() - startTime));

            return 1;

        } catch (Exception e) {
            log.error("3Vjia电话监控页面统计定时任务操作异常", e);
            return 0;
        }
    }

}